package com.hp.gxw.api;

import com.alibaba.fastjson.JSON;
import com.hp.gxw.constant.R;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.RdRecord01Parameter;
import com.hp.gxw.u8.dto.RdRecords01Parameter;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.xml.RdRecord01Xml;
import com.hp.gxw.utils.DateUtil;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RequestMapping("/api/rdrecord01")
public class RdRecord01Api {

    @Autowired
    private U8ApiUtils u8ApiUtils;

	@Autowired
	private EFWebService eFWebServiceLocator;

    @Value("${u8client.serviceId}")
    private String serviceId;

    @Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R rdrecord01_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        RdRecord01Parameter rdRecord01Parameter = new RdRecord01Parameter();
        if(map.get("cvouchtype") != null){
            rdRecord01Parameter.setCvouchtype(map.get("cvouchtype").toString());
        }else{
            rdRecord01Parameter.setCvouchtype("01");
        }
        if(map.get("cbustype") != null){
            rdRecord01Parameter.setCbustype(map.get("cbustype").toString());
        }else{
            rdRecord01Parameter.setCbustype("普通采购");
        }
        if(map.get("cptcode") == null){
            return R.error("cptcode为必填项");
        }
        rdRecord01Parameter.setCptcode(map.get("cptcode").toString());//采购类型
        if(map.get("csource") != null){
            rdRecord01Parameter.setCsource(map.get("csource").toString());
        }else{
            rdRecord01Parameter.setCsource("采购订单");
        }
        if(map.get("cwhcode") == null){
            return R.error("cwhcode为必填项");
        }
        rdRecord01Parameter.setCwhcode(map.get("cwhcode").toString());
        if(map.get("ccode") != null){
            rdRecord01Parameter.setCcode(map.get("ccode").toString());
        }else{
            rdRecord01Parameter.setCcode("0000000001");
        }
        if(map.get("cordercode") != null){
            rdRecord01Parameter.setCordercode(map.get("cordercode").toString());
        }
        if(map.get("cbarvcode") != null){
            rdRecord01Parameter.setCbarvcode(map.get("cbarvcode").toString());
        }
        if(map.get("cvencode") == null){
            return R.error("cvencode为必填项");
        }
        rdRecord01Parameter.setCvencode(map.get("cvencode").toString());
        if(map.get("cdepcode") != null){
            rdRecord01Parameter.setCdepcode(map.get("cdepcode").toString());
        }
        if(map.get("ddate") != null){
            rdRecord01Parameter.setDdate(DateUtil.getDateFormat(map.get("ddate").toString()));
        }else{
            rdRecord01Parameter.setDdate(new Date());
        }
        if(map.get("cmemo") != null){
            rdRecord01Parameter.setCmemo(map.get("cmemo").toString());
        }
        if(map.get("dnverifytime") != null){
            rdRecord01Parameter.setDnverifytime(DateUtil.getDateTimeFormat(map.get("dnverifytime").toString()));
        }else{
            rdRecord01Parameter.setDnverifytime(new Date());
        }
        if(map.get("carvcode") != null){
            rdRecord01Parameter.setCarvcode(map.get("carvcode").toString());
        }
        if(map.get("cmaker") != null){
            rdRecord01Parameter.setCmaker(map.get("cmaker").toString());
        }else{
            rdRecord01Parameter.setCmaker("api");
        }
        if(map.get("chandler") != null){
            rdRecord01Parameter.setChandler(map.get("chandler").toString());
        }else{
            rdRecord01Parameter.setChandler("api");
        }
        if(map.get("cpersoncode") != null){
            rdRecord01Parameter.setCpersoncode(map.get("cpersoncode").toString());
        }
        if(map.get("dveridate") != null){
            rdRecord01Parameter.setDveridate(DateUtil.getDateFormat(map.get("dveridate").toString()));
        }else{
            rdRecord01Parameter.setDveridate(new Date());
        }
        if(map.get("iexchrate") != null){
            rdRecord01Parameter.setIexchrate(map.get("iexchrate").toString());
        }else{
            rdRecord01Parameter.setIexchrate("1");
        }
        if(map.get("cexch_name") != null){
            rdRecord01Parameter.setCexch_name(map.get("cexch_name").toString());
        }else{
            rdRecord01Parameter.setCexch_name("人民币");
        }
        if(map.get("dnmaketime") != null){
            rdRecord01Parameter.setDnmaketime(DateUtil.getDateTimeFormat(map.get("dnmaketime").toString()));
        }else{
            rdRecord01Parameter.setDnmaketime(new Date());
        }

        if(map.get("details") == null){
            return R.error("表体详情数据不能为空");
        }
        List<RdRecords01Parameter> rdRecords01ParameterList = JSON.parseArray(JSON.toJSON(map.get("details")).toString(), RdRecords01Parameter.class);
        for(int i=0; i<rdRecords01ParameterList.size(); i++){
            if(rdRecords01ParameterList.get(i).getCinvcode() == null){
                return R.error("表体详情数据第"+i+1+"行cinvcode为空");
            }
            if(rdRecords01ParameterList.get(i).getIquantity() == null || rdRecords01ParameterList.get(i).getIquantity() == 0){
                return R.error("表体详情数据第"+i+1+"行iquantity为空或者为0");
            }
            if(rdRecords01ParameterList.get(i).getCpoid() == null){
                return R.error("表体详情数据第"+i+1+"行cpoid为空");
            }
            if(rdRecords01ParameterList.get(i).getIposid() == null){
                return R.error("表体详情数据第"+i+1+"行iposid为空");
            }
            if(rdRecords01ParameterList.get(i).getCbarvdate() == null){
                rdRecords01ParameterList.get(i).setCbarvdate(new Date());
            }
        }
        rdRecord01Parameter.setRdRecords01ParameterList(rdRecords01ParameterList);
        rdRecord01Parameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(RdRecord01Xml.createXML(rdRecord01Parameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(RdRecord01Xml.createXML(rdRecord01Parameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
